package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.management.aop.annotation.LogAnno;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/roles")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping
    public Result page(
            @RequestParam(name="page",required = true,defaultValue = "1")Integer page,
            @RequestParam(name="pageSize",required = true,defaultValue = "10") Integer pageSize,
            @RequestParam(name="name",required = false) String name,
            @RequestParam(name="label",required = false) String label,
            @RequestParam(name="remark",required = false) String remark){
        log.info("分页查询角色: {}",page);

        PageResult<Role> pageResult = roleService.page(page, pageSize, name, label, remark);

        return Result.success(pageResult);
    }

    @LogAnno
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除角色: {}",id);
        roleService.deleteById(id);
        return Result.success();
    }

    @LogAnno
    @PostMapping
    public Result add(@RequestBody Role role){
        log.info("添加角色: {}",role);
        roleService.add(role);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询角色: {}",id);
        Role role = roleService.getById(id);
        return Result.success(role);
    }

    @LogAnno
    @PutMapping
    public Result update(@RequestBody Role role){
        log.info("修改角色: {}",role);
        roleService.update(role);
        return Result.success();
    }


    /*
    *
    * 6.1 基本信息
请求路径：/roles/list
请求方式：GET
接口描述：该接口用于查询所有角色数据*/
    @GetMapping("/list")
    public Result findAll(){
        log.info("查询角色列表");
        List<Role> rs = roleService.findAll();
        return Result.success(rs);
    }
}